﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace DIProject.Util
{
    public class Validator
    {
        private static Regex reg = new Regex("^\\d+(\\.\\d+)?$", RegexOptions.IgnoreCase);
        private static System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.Number;
        private static System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
        public static bool Int32Validation(System.Windows.Forms.TextBox tb, string errorMesage)
        {
            Int32 i;
            string em = errorMesage;
            if (string.IsNullOrEmpty(em))
                em = "Введите целое число";
            if (string.IsNullOrEmpty(tb.Text))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            if (!Int32.TryParse(tb.Text,out i))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            return true;
        }
        public static bool DecimalValidation(System.Windows.Forms.TextBox tb, string errorMesage)
        {
            decimal i;
            string em = errorMesage;
            if (string.IsNullOrEmpty(em))
                em = "Введите действительное число";
            if (string.IsNullOrEmpty(tb.Text))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            string sts = tb.Text.Replace(',','.');
            if (!reg.IsMatch(sts))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            if (!decimal.TryParse(tb.Text, ns, ci, out i))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            return true;
        }
        public static bool DoubleValidation(System.Windows.Forms.TextBox tb, string errorMesage)
        {
            double i;
            string em = errorMesage;
            if (string.IsNullOrEmpty(em))
                em = "Введите вещественное число";
            if (string.IsNullOrEmpty(tb.Text))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            string sts = tb.Text.Replace(',', '.');
            if (!reg.IsMatch(sts))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            if (!double.TryParse(tb.Text, ns, ci, out i))
            {
                ShowError(em);
                tb.Focus();
                return false;
            }
            return true;
        }
        private static void ShowError(string errorText)
        {
            System.Windows.Forms.MessageBox.Show(errorText, "Ошибка", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }
    }
}
